package com.boom.search.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.time.Duration;

/**
 * created with IntelliJ IDEA.
 *
 * @author yzw
 * @date 2021/6/23 上午9:01
 * @description
 */
@Configuration
@EnableElasticsearchRepositories
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

	@Value("${spring.elasticsearch.rest.uris}")
	private String uris;

	@Override
	@Bean
	public RestHighLevelClient elasticsearchClient() {
		final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
				.connectedTo(uris.split(","))
				.withConnectTimeout(Duration.ofSeconds(5))
				.withSocketTimeout(Duration.ofSeconds(30))
				.build();
		return RestClients.create(clientConfiguration).rest();
	}

	@Bean("esRestTemplate")
	public ElasticsearchRestTemplate esRestTemplate() {
		return new ElasticsearchRestTemplate(elasticsearchClient());
	}
}
